package academic.ui.client.presenter;




import java.util.ArrayList;
import java.util.List;

import ui.client.GenericClickable;

import academic.ui.client.AppServiceAsync;
import academic.ui.client.place.Tokens;
import academic.ui.client.widgets.tools.Scrollable;
import ecommerce.soco.shared.GWT_Product;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.ProxyPlace;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;

public class RecentDealsPresenter extends Presenter<RecentDealsPresenter.MyView,RecentDealsPresenter.MyProxy>
{
	
	private final PlaceManager placeManager;
	
	private AppServiceAsync service;
	@Inject 
	public RecentDealsPresenter(EventBus eventBus, MyView view, MyProxy proxy,
			AppServiceAsync service,PlaceManager placeManager) {
		super(eventBus, view, proxy);
		this.placeManager=placeManager;
		this.service=service;
	}

	@ProxyStandard	
	@NameToken(Tokens.recentDeals)
	public interface MyProxy extends ProxyPlace<RecentDealsPresenter>{

	}
	
	public interface MyView extends View {
		public void setup(List<GWT_Product> deals);

		public ArrayList<GenericClickable> getHandlers();

		void hide();
	}
	
	
	@Override
	public void onReveal() {
		super.onReveal();
		Log.debug("RecentDealsPresenter onReveal");
		service.getRecentProducts(new AsyncCallback<ArrayList<GWT_Product>>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(ArrayList<GWT_Product> result) {
				getView().setup(result);
				for(GenericClickable clickable : getView().getHandlers())
				{
					registerHandler(clickable.addClickHandler(new ClickHandler() {

						@Override
						public void onClick(ClickEvent event) {
							placeManager.revealPlace(new PlaceRequest(Tokens.productFull).with("id", event.getRelativeElement().getId()).with("expired", "true"));
						}
						
					}));
				}
				
				DeferredCommand.addCommand(new Command() {

					@Override
					public void execute() {
						Scrollable.initScrollable();
						Log.debug("RecentDealsPresenter onReveal DeferredCommand");
					}
					
				});
			}
			
		});
		
	}
	
	@Override
	public void onBind() {
		super.onBind();
		Log.debug("RecentDealsPresenter onBind");
	}
	
	@Override
	public void onUnbind() {
		super.onUnbind();
		Log.debug("RecentDealsPresenter onUnbind");
	}
	
    
	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, AppPresenter.slot, this);
		Log.debug("RecentDealsPresenter revealInParent");
	}
	
	@Override
	public void onHide() {
		//getView().hide();
		Log.debug("RecentDealsPresenter onHide");
	}
	
	

}
